//
//  Common.m
//  RssRead
//
//  Created by 旺财 on 10-11-10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Common.h"
#import <CommonCrypto/CommonDigest.h>


@implementation Common

static GDataXMLElement *rootE = nil;
static NSMutableArray *listE=nil;
static Config *config;

+(NSString *) md5:(NSString *)str {
	const char *cStr = [str UTF8String];
	unsigned char result[16];
	CC_MD5( cStr, strlen(cStr), result );
	return [NSString stringWithFormat:
			@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]
			]; 
}


+ (NSString *)flattenHTML:(NSString *)html {
	
    NSScanner *theScanner;
    NSString *text = nil;
	NSString *orhtml=[html copy];
	
	@try
	{
		theScanner = [NSScanner scannerWithString:html];
		
		while ([theScanner isAtEnd] == NO) {
			
			// find start of tag
			[theScanner scanUpToString:@"<" intoString:NULL] ; 
			
			// find end of tag
			[theScanner scanUpToString:@">" intoString:&text] ;
			
			
			// replace the found tag with a space
			//(you can filter multi-spaces out later if you wish)
			html = [html stringByReplacingOccurrencesOfString:
					[ NSString stringWithFormat:@"%@>", text]
												   withString:@""];
			html = [html stringByReplacingOccurrencesOfString:
					[ NSString stringWithFormat:@"&nbsp;"]
												   withString:@""];
			
		} // while //
    }
	@catch(NSException *e)
	{
		return orhtml;
	}
    return html;
	
} 


+(NSString *)getRootDocDir
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	return  [paths objectAtIndex:0];
}
+(GDataXMLElement *)getrootE
{
	
	return rootE;
}
+(void)setroot:(GDataXMLElement *) roote
{
	//[rootE dealloc];
	rootE=[roote copy];
}
+(NSMutableArray *) getlistE
{
	return listE;
}
+(void)setlistE:(NSMutableArray *) liste
{
	listE=[liste copy];
}

+(void) initDir:(NSString *) path
{
	if (![[NSFileManager defaultManager]  fileExistsAtPath:path])
		[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
	return;
}

+(void) initOPMLby:(NSString *) resouceOPML to:rootOPML
{
	if (![[NSFileManager defaultManager] fileExistsAtPath:rootOPML])
	{	
		[[NSFileManager defaultManager] copyItemAtPath:resouceOPML toPath:rootOPML error:nil];
	}
}

+(Config *) getConfig
{
	return config;
}
+(void)initConfig
{
	config=[[Config alloc] init];
	[config setCacheDir:@"mycache"];
	[config setRootDir:[Common getRootDocDir]];
	config.cacheFullDir=[[NSString alloc] initWithFormat:@"%@/%@",config.rootDir,config.cacheDir];
	[config setRootOPMLFilePath:@"op.xml"];
	config.rootOPMLFileFullPath=[[NSString alloc] initWithFormat:@"%@/%@",config.rootDir,config.rootOPMLFilePath];
	[config setResourceOMPLfielpath:[[NSBundle mainBundle] pathForResource:@"op" ofType:@"xml"]];
	[config setBaseURL:@"http://59.151.3.101/rss/"];
	[config setRecommendedURL:@"http://59.151.3.101/rss/wc.xml"];
	[config setRecommendedVerURL:@"http://59.151.3.101/rss/ver.html"];
	NSURL *url=[NSURL URLWithString:[config recommendedVerURL]];
	[config setRecommendedVer:[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]];
	 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
	//
	[config setUpdateRSSTime: [[defaults valueForKey:@"updateRSS"] integerValue]];
	[config setRecommendedLocalVerFilePath:[NSString stringWithFormat:@"%@/%@",config.rootDir,@"/ver.info"]];
	[config setRecommendedLocalVer:[NSString stringWithContentsOfFile:config.recommendedLocalVerFilePath encoding:NSUTF8StringEncoding 
																error:nil]];
	[config setDefaultOpenWithRss: [[defaults valueForKey:@"defaultOpen"] boolValue]];
	[config setRestore: [[defaults valueForKey:@"Restore"] boolValue]];
	[config setCanRecommended: [[defaults valueForKey:@"wctj"] boolValue]];
	//return config;
}

@end
